----------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    梅菜扣肉

]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 素材
    local assets = {
        Asset("ANIM", "anim/hutao_cooked_food_braised_pork_belly_with_preserved_vegetable.zip"), 
        Asset( "IMAGE", "images/inventoryimages/hutao_cooked_food_braised_pork_belly_with_preserved_vegetable.tex" ),  -- 背包贴图
        Asset( "ATLAS", "images/inventoryimages/hutao_cooked_food_braised_pork_belly_with_preserved_vegetable.xml" ),
    }
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 食物本体
    local function food_fn()

        local inst = CreateEntity() -- 创建实体
        inst.entity:AddTransform() -- 添加xyz形变对象
        inst.entity:AddAnimState() -- 添加动画状态
        inst.entity:AddNetwork() -- 添加这一行才能让所有客户端都能看到这个实体

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("hutao_cooked_food_braised_pork_belly_with_preserved_vegetable") -- 地上动画
        inst.AnimState:SetBuild("hutao_cooked_food_braised_pork_belly_with_preserved_vegetable") -- 材质包，就是anim里的zip包
        inst.AnimState:PlayAnimation("idle") -- 默认播放哪个动画

        MakeInventoryFloatable(inst)
        inst:AddTag("preparedfood")

        inst.entity:SetPristine()
        
        if not TheWorld.ismastersim then
            return inst
        end
        --------------------------------------------------------------------------
        ------ 物品名 和检查文本
        inst:AddComponent("inspectable")

        inst:AddComponent("inventoryitem")
        -- inst.components.inventoryitem:ChangeImageName("leafymeatburger")
        inst.components.inventoryitem.imagename = "hutao_cooked_food_braised_pork_belly_with_preserved_vegetable"
        inst.components.inventoryitem.atlasname = "images/inventoryimages/hutao_cooked_food_braised_pork_belly_with_preserved_vegetable.xml"

        --------------------------------------------------------------------------


        inst:AddComponent("edible") -- 可食物组件
        inst.components.edible.foodtype = FOODTYPE.MEAT
        inst.components.edible:SetOnEatenFn(function(inst,eater)
            if eater and eater:HasTag("hutao") then
                eater:AddDebuff("hutao_debuff_braised_pork_belly_with_preserved_vegetable","hutao_debuff_braised_pork_belly_with_preserved_vegetable")
                eater.components.health:DoDelta(inst.components.edible.healthvalue,nil,inst.prefab) -- debuff 增加上限后，需要再次恢复current值。
                eater.components.hutao_com_dialogue:Say("eaten.braised_pork_belly_with_preserved_vegetable")
            end
        end)
        inst.components.edible.hungervalue = 77
        inst.components.edible.sanityvalue = 0
        inst.components.edible.healthvalue = 154

        inst:AddComponent("perishable") -- 可腐烂的组件
        inst.components.perishable:SetPerishTime(14*TUNING.PERISH_ONE_DAY)
        inst.components.perishable:StartPerishing()
        inst.components.perishable.onperishreplacement = "spoiled_food" -- 腐烂后变成腐烂食物


        inst:AddComponent("stackable") -- 可堆叠
        inst.components.stackable.maxsize = TUNING.STACK_SIZE_LARGEITEM
        inst:AddComponent("tradable")

        MakeHauntableLaunch(inst)
        -------------------------------------------------------------------
        --- 落水影子
            local function shadow_init(inst)
                if inst:IsOnOcean(false) then       --- 如果在海里（不包括船）
                    inst.AnimState:Hide("SHADOW")
                else                                
                    inst.AnimState:Show("SHADOW")
                end
            end
            inst:ListenForEvent("on_landed",shadow_init)
            shadow_init(inst)
        -------------------------------------------------------------------
        
        return inst
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- debuff
    local DEBUFF_TIMER_DELTA = 0.2              -- debuff 刷新检查时间
    local DEBUFF_START_TIME = 343               -- 初始 debuff 时间
    local DEBUFF_MAX_HEALTH_PER_LEVEL = 49      -- 每级增加的血量上限

    local function OnAttached(inst,target) -- 玩家得到 debuff 的瞬间。 穿越洞穴、重新进存档 也会执行。
        inst.entity:SetParent(target.entity)
        -- inst.Network:SetClassifiedTarget(target)
        inst.Transform:SetPosition(0,0,0)
        inst.player = target
        -----------------------------------------------------
        --- 控制器检查
            if target.components.hutao_com_max_value_controller == nil then
                inst:Remove()
                return
            end
        -----------------------------------------------------
        --- 计时器初始化
            if inst.components.hutao_data:Add("time",0) == 0 then
                inst.components.hutao_data:Set("time",DEBUFF_START_TIME)
                target.components.hutao_com_inspect_data:Set("hutao_debuff_braised_pork_belly_with_preserved_vegetable",DEBUFF_START_TIME)
            end
            inst:DoPeriodicTask(DEBUFF_TIMER_DELTA,function()
                local level = inst.components.hutao_data:Add("level",0)
                local ret_time = inst.components.hutao_data:Add("time",-DEBUFF_TIMER_DELTA*level)
                target.components.hutao_com_inspect_data:Set("hutao_debuff_braised_pork_belly_with_preserved_vegetable",ret_time)
                if ret_time <= 0 then
                    inst:Remove()
                    if target.components.hutao_com_dialogue then
                        target.components.hutao_com_dialogue:Say("eaten.debuff_remove.braised_pork_belly_with_preserved_vegetable")
                    end

                end
                -- print("梅菜扣肉debuff剩余时间：",level,ret_time)
            end)
            inst:ListenForEvent("onremove",function()
                target.components.hutao_com_inspect_data:Set("hutao_debuff_braised_pork_belly_with_preserved_vegetable",0)                
            end)
        -----------------------------------------------------
        --- 等级刷新
            inst:ListenForEvent("refresh_debuff",function(inst)
                --- 等级从1开始。
                local level = inst.components.hutao_data:Add("level",0)
                if level == 0 then
                    level = inst.components.hutao_data:Add("level",1)
                end
                --- 
                target.components.hutao_com_max_value_controller:AddTempExtraHealth(inst,level*DEBUFF_MAX_HEALTH_PER_LEVEL)
            end)
        -----------------------------------------------------
        --- 加载初始化。
            inst:DoTaskInTime(0,function()
                inst:PushEvent("refresh_debuff")
            end)
            inst:PushEvent("refresh_debuff")
        -----------------------------------------------------
    end

    local function ExtendDebuff(inst)
        inst.components.hutao_data:Add("level",1)
        inst:PushEvent("refresh_debuff")
    end

    local function debuff_fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddNetwork()
        inst.entity:AddTransform()

        inst:AddTag("CLASSIFIED")

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        inst:AddComponent("hutao_data")
        inst:AddComponent("debuff")
        inst.components.debuff:SetAttachedFn(OnAttached)
        inst.components.debuff.keepondespawn = true -- 是否保持debuff 到下次登陆
        inst.components.debuff:SetExtendedFn(ExtendDebuff)

        return inst
    end
----------------------------------------------------------------------------------------------------------------------------------------------------

return Prefab("hutao_cooked_food_braised_pork_belly_with_preserved_vegetable", food_fn, assets),
    Prefab("hutao_debuff_braised_pork_belly_with_preserved_vegetable", debuff_fn, assets)